import React, { Component } from 'react';
import {
    View,
    Image,
    ScrollView,
    StyleSheet,
} from 'react-native'
import { connect } from 'react-redux';
import { bindActionCreators } from 'redux';

import moment from 'moment'
import * as fetch from '../../utils/fetch'
import Style from '../../utils/style/style'
import Loading from '../../views/loading';
import CellItem from '../../views/cellItem';
import CheckStateItem from '../../views/checkStateItem';
import CellTitleItem from '../../views/cellTitleItem';
import SerialsItem from '../../views/serialsItem';
import GoodItem from '../../views/goodItem';
import { resultInitialData, resultGetGoods,fetchSerails } from '../../actions/CheckAction';
class Result extends Component {
    constructor(props) {
        super(props)
    }
    static navigationOptions = {
        title: '盘点结果',
    };
    componentDidMount() {
        const { navigation } = this.props;
        const InventoryId = navigation.getParam("InventoryId");
        const inv_code = navigation.getParam("inv_code");
        const inv_time = navigation.getParam("inv_time");
        const item = JSON.parse(navigation.getParam("item"));
        let itemTitle;
        switch (item.state) {
            case 1:
                itemTitle = "缺失的商品";
                break;
            case 2:
                itemTitle = "冗余的商品";
                break;
            case 3:
                itemTitle = "未入库无法识别的序列号";
                break;
            case 4:
                itemTitle = "正常的商品";
                break;
            default:
                break
        }
        let params = {
            InventoryId: InventoryId,
            inv_code: inv_code,
            inv_time: inv_time,
            item: item,
            itemTitle: itemTitle
        }
        const api = "erp_archivess";
        this.props.resultInitialData(params);
        this._fetchDetail(params)

    }

    _fetchDetail(paramsP) {
        const { InventoryId,item } = paramsP
        if (item.state != 3) {

            this.props.resultGetGoods();
        } else {
            const params = {
                api:'erp_storehouse',
                filter: "InventoryId eq " + InventoryId + " and store_type eq " + item.state,
                select: "BarCode"
            }
            this.props.fetchSerails(params);
        }
    }

    _renderGood(item, index, length) {
        let valueItem = {};
        const data = this.props.item;
        const detailList = [
            { key: '货号', value: item.Code },
            { key: '当前库存', value: item.stocks },
            { key: '盘点数量', value: item.invNumber },
            { key: data.title + '数量', value: item.count, color: data.color }
        ]
        const noBorder = index == length - 1 ? true : false
        return (
            <GoodItem
                source={item.images[0]}
                name={item.Name}
                key={item.name}
                showAngle={true}
                detailList={detailList}
                pressItem={() => { this._pressGoodItem(item, detailList) }}
                noBorder={noBorder}
            />
        )

    }
    _pressGoodItem(item, detailList) {
        item.detailList = detailList;
        const itemStr = JSON.stringify(item);
        const { inv_code, inv_time } = this.props;
        this.props.navigation.navigate('ResultSerials', { item: itemStr, inv_code: inv_code, inv_time: inv_time, state: this.props.item.state })
    }

    _renderSerails(item, index, length) {
        const noBorder = index == length - 1 ? true : false;
        return (
            <SerialsItem value={item.BarCode} noBorder={noBorder} />
        )
    }

    render() {
        let time;
        if (this.props.inv_time) {
            time = moment(this.props.inv_time).format("YYYY-MM-DD HH:mm")
        }
        return (
            !this.props.inv_code || !this.props.inv_time || !this.props.item ? null :
                <ScrollView style={styles.container}>
                    <View style={Style.gap} />
                    <CellItem title="盘点号" content={this.props.inv_code} />
                    <CellItem title="盘点时间" content={time} noBorder={true} />
                    <View style={[Style.gap, styles.gapTop]} />
                    <CheckStateItem
                        item={this.props.item}
                        showAngle={false}
                        noBorder={true}
                        pressItem={() => { return }}
                    />
                    <CellTitleItem title={this.props.itemTitle} />
                    <View style={styles.content}>
                        {
                            this.props.loading ?
                                null : this.props.dataList ?
                                    this.props.item.state == 3 ?
                                        this.props.dataList.map((item, index) => this._renderSerails(item, index, this.props.dataList.length))
                                        :
                                        this.props.dataList.map((item, index) => this._renderGood(item, index, this.props.dataList.length)
                                        )
                                    : null

                        }
                    </View>
                    <View style={Style.bottom} />
                </ScrollView>
        )
    }
}

const styles = StyleSheet.create({
    container: {
        width: Style.width,
        height: Style.ViewHeightD,
        backgroundColor: '#fff',
    },
    gapTop: {
        borderTopWidth: 1,
    },

    content: {

    },

})

export default connect(
    state => ({
        loading: state.Result.loading,
        InventoryId: state.Result.InventoryId,
        inv_code: state.Result.inv_code,
        inv_time: state.Result.inv_time,
        item: state.Result.item,
        itemTitle: state.Result.itemTitle,
        dataList: state.Result.dataList
    }),
    dispatch => ({
        resultInitialData: bindActionCreators(resultInitialData, dispatch),
        resultGetGoods: bindActionCreators(resultGetGoods, dispatch),
        fetchSerails: bindActionCreators(fetchSerails, dispatch)
    })
)(Result)